#include "login.h"


// 全局变量
static lv_obj_t *msgbox = NULL;
lv_obj_t *scr_login = NULL;

lv_font_t *font_chinese;
lv_obj_t *scr_main = NULL;

lv_obj_t *user_ta;
lv_obj_t *pwd_ta;

const char *user=NULL;

// 创建主界面
void create_main_screen(void) 
{
    scr_main = lv_obj_create(NULL);
    lv_obj_add_style(scr_main, &bg_style, 0);
    // 初始化主界面
    init_main_screen();
}


// 消息框关闭事件回调
static void msgbox_event_cb(lv_event_t *e) {
    lv_obj_t *obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED || code == LV_EVENT_VALUE_CHANGED) 
    {
        if(msgbox != NULL) {
            lv_obj_del(msgbox);
            msgbox = NULL;
        }
    }
}

// 显示消息框
static void show_message(const char *title, const char *message, lv_color_t bg_color) {
    // 如果已有消息框，先删除
    if(msgbox != NULL) {
        lv_obj_del(msgbox);
        msgbox = NULL;
    }
    
    // 创建自定义消息框
    msgbox = lv_obj_create(lv_scr_act());
    lv_obj_set_size(msgbox, 250, 150);
    lv_obj_align(msgbox, LV_ALIGN_CENTER, 0, 0);
    
    // 设置消息框样式
    lv_obj_set_style_bg_color(msgbox, bg_color, 0);
    lv_obj_set_style_bg_opa(msgbox, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(msgbox, 2, 0);
    lv_obj_set_style_border_color(msgbox, lv_color_white(), 0);
    lv_obj_set_style_radius(msgbox, 10, 0);
    lv_obj_set_style_pad_all(msgbox, 10, 0);
    
    // 添加标题
    lv_obj_t *title_label = lv_label_create(msgbox);
    lv_label_set_text(title_label, title);
    lv_obj_set_style_text_font(title_label, font_chinese, 0);
    lv_obj_set_style_text_color(title_label, lv_color_white(), 0);
    lv_obj_set_style_text_align(title_label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 10);
    
    // 添加消息
    lv_obj_t *msg_label = lv_label_create(msgbox);
    lv_label_set_text(msg_label, message);
    lv_obj_set_style_text_font(msg_label, font_chinese, 0);
    lv_obj_set_style_text_color(msg_label, lv_color_white(), 0);
    lv_obj_set_style_text_align(msg_label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(msg_label, LV_ALIGN_CENTER, 0, 0);
    
    // 添加确定按钮
    lv_obj_t *ok_btn = lv_btn_create(msgbox);
    lv_obj_set_size(ok_btn, 80, 35);
    lv_obj_align(ok_btn, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_style_bg_color(ok_btn, lv_color_hex(0x444444), 0);
    lv_obj_set_style_radius(ok_btn, 5, 0);
    
    // 添加确定按钮文本
    lv_obj_t *ok_label = lv_label_create(ok_btn);
    lv_label_set_text(ok_label, "确定");
    lv_obj_set_style_text_font(ok_label, font_chinese, 0);
    lv_obj_set_style_text_color(ok_label, lv_color_white(), 0);
    lv_obj_center(ok_label);
    
    // 为确定按钮添加事件回调
    lv_obj_add_event_cb(ok_btn, msgbox_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 为整个消息框添加点击事件
    lv_obj_add_event_cb(msgbox, msgbox_event_cb, LV_EVENT_CLICKED, NULL);
}

// 登录事件回调函数
static void login_event_cb(lv_event_t *e) 
{
    
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *user_ta = (lv_obj_t *)lv_event_get_user_data(e);
    lv_obj_t *pwd_ta = (lv_obj_t *)lv_obj_get_user_data(btn);
    
    // 获取用户名和密码
    user = lv_textarea_get_text(user_ta);
    const char *pwd = lv_textarea_get_text(pwd_ta);
    
    // 验证账号密码
    int result = verifyUser(userList, user, pwd);
    
    if(result == 1) 
    {
        // 登录成功，切换到主界面
        lv_scr_load(scr_main);
        
        // 保存用户数据到文件
        saveUsersToFile(userList, "userManagement.json");
    } else if(result == -1) {
        // 用户被封禁
        show_message("错误", "账号已被封禁!", lv_color_hex(0xFF5555));
    } else {
        // 登录失败
        show_message("错误", "账号或密码不正确!", lv_color_hex(0xFF5555));
        
        // 清空输入框
        lv_textarea_set_text(user_ta, "");
        lv_textarea_set_text(pwd_ta, "");
    }
}

// 注册事件回调函数
static void register_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *user_ta = (lv_obj_t *)lv_event_get_user_data(e);
    lv_obj_t *pwd_ta = (lv_obj_t *)lv_obj_get_user_data(btn);
    
    // 获取用户名和密码
    const char *user = lv_textarea_get_text(user_ta);
    const char *pwd = lv_textarea_get_text(pwd_ta);
    
    // 检查用户名和密码是否为空
    if(strlen(user) == 0 || strlen(pwd) == 0) 
    {
        show_message("错误", "用户名和密码不能为空!", lv_color_hex(0xFF5555));
        return;
    }
    
    // 注册新用户
    if(registerUser(userList, user, pwd)) 
    {
        // 注册成功
        show_message("成功", "注册成功!", lv_color_hex(0x55FF55));
        
        // 保存用户数据到文件
        saveUsersToFile(userList, "userManagement.json");
        
        // 清空输入框
        lv_textarea_set_text(user_ta, "");
        lv_textarea_set_text(pwd_ta, "");
    } else {
        // 注册失败
        show_message("错误", "用户名已存在!", lv_color_hex(0xFF5555));
    }
}

// 输入框事件回调
static void ta_event_cb(lv_event_t *e)
 {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);
    
    if(code == LV_EVENT_FOCUSED) 
    {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_move_foreground(kb);
    }

    if(code == LV_EVENT_DEFOCUSED) 
    {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
}

// // 背景色样式初始化
// void bg_style_init(void) 
// {
//     lv_style_init(&bg_style);
//     lv_style_set_bg_color(&bg_style, lv_color_hex(0x1A1A24));
//     lv_style_set_bg_grad_color(&bg_style, lv_color_hex(0x0D0D15));
//     lv_style_set_bg_grad_dir(&bg_style, LV_GRAD_DIR_VER);
//     lv_style_set_border_color(&bg_style, lv_color_hex(0xEEEEEE));
//     lv_style_set_border_width(&bg_style, 1);
// }

// 中文字体样式初始化
void chineseFontInitialization(void) 
{
    static lv_ft_info_t info;
    info.name = "/userdata/zjy/simhei.ttf";
    // info.name = "/zjy/simhei.ttf";
    info.weight = 20;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }
    
    font_chinese = NULL;
    font_chinese = info.font;
}


// 登录界面初始化
void loginUI(void) 
{
    // 初始化用户链表
    userList = initUserList();
    if(userList) 
    {
        // 从文件加载用户数据
        loadUsersFromFile(userList, "userManagement.json");
    }
    //背景色初始化
    bg_style_init();
    //字体初始化
    chineseFontInitialization();
    //主界面初始化
    create_main_screen(); 
    
    // 创建登录界面
    scr_login = lv_obj_create(NULL);
    lv_obj_add_style(scr_login, &bg_style, 0);
    lv_scr_load(scr_login); 
    
    // 创建标题
    lv_obj_t *title = lv_label_create(scr_login);
    lv_label_set_text(title, "购票系统登录");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_set_style_text_opa(title, LV_OPA_COVER, 0);
    
    // 用户名输入框
    user_ta = lv_textarea_create(scr_login);
    lv_obj_align(user_ta, LV_ALIGN_TOP_MID, 0, 100);
    lv_textarea_set_placeholder_text(user_ta, "请输入账号");
    lv_obj_set_style_text_font(user_ta, font_chinese, 0);
    lv_obj_set_size(user_ta, 200, 40);
    lv_textarea_set_max_length(user_ta, 20);
    lv_textarea_set_one_line(user_ta, true);
    
    // 密码输入框
    pwd_ta = lv_textarea_create(scr_login);
    lv_obj_align(pwd_ta, LV_ALIGN_TOP_MID, 0, 160);
    lv_textarea_set_placeholder_text(pwd_ta, "请输入密码");
    lv_obj_set_style_text_font(pwd_ta, font_chinese, 0);
    lv_obj_set_size(pwd_ta, 200, 40);
    lv_textarea_set_max_length(pwd_ta, 20);
    lv_textarea_set_one_line(pwd_ta, true);
    lv_textarea_set_password_mode(pwd_ta, true);
    lv_textarea_set_password_show_time(pwd_ta, 1000);
    
    // 键盘
    lv_obj_t *kb = lv_keyboard_create(scr_login);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    
    // 为输入框添加事件回调
    lv_obj_add_event_cb(user_ta, ta_event_cb, LV_EVENT_ALL, kb);
    lv_obj_add_event_cb(pwd_ta, ta_event_cb, LV_EVENT_ALL, kb);
    
    // 登录按钮
    lv_obj_t *login_btn = lv_btn_create(scr_login);
    lv_obj_align(login_btn, LV_ALIGN_TOP_MID, 0, 220);
    lv_obj_set_size(login_btn, 190, 40);
    lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x007ACC), 0);
    
    // 存储密码输入框引用到登录按钮的用户数据中
    lv_obj_set_user_data(login_btn, pwd_ta);
    
    // 登录按钮标签
    lv_obj_t *login_label = lv_label_create(login_btn);
    lv_label_set_text(login_label, "登录");
    lv_obj_set_style_text_font(login_label, font_chinese, 0);
    lv_obj_center(login_label);
    
    // 为登录按钮添加事件回调
    lv_obj_add_event_cb(login_btn, login_event_cb, LV_EVENT_CLICKED, user_ta);
    
    // 注册按钮
    lv_obj_t *register_btn = lv_btn_create(scr_login);
    lv_obj_align(register_btn, LV_ALIGN_TOP_MID, 0, 270);
    lv_obj_set_size(register_btn, 190, 40);
    lv_obj_set_style_bg_color(register_btn, lv_color_hex(0x28A745), 0);
    
    // 存储密码输入框引用到注册按钮的用户数据中
    lv_obj_set_user_data(register_btn, pwd_ta);
    
    // 注册按钮标签
    lv_obj_t *register_label = lv_label_create(register_btn);
    lv_label_set_text(register_label, "注册");
    lv_obj_set_style_text_font(register_label, font_chinese, 0);
    lv_obj_center(register_label);
    
    // 为注册按钮添加事件回调
    lv_obj_add_event_cb(register_btn, register_event_cb, LV_EVENT_CLICKED, user_ta);

}

